package com.zhang.dao;


import com.zhang.pojo.User;
import com.zhang.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.type.Alias;
import org.junit.Test;

import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.util.List;
import java.util.UUID;

public class UserTestDao {

    @Test
    public void test(){
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        SqlSession sqlSession1 = MybatisUtils.getSqlSession();
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        UserMapper mapper1 = sqlSession1.getMapper(UserMapper.class);

        User user = mapper.queryUserById(1);
        sqlSession.close();
        User user1 = mapper1.queryUserById(1);
        System.out.println(user==user1);
        System.out.println(user1);
        System.out.println("-=========================-");
//        User user2 = mapper.queryUserById(1);
//        System.out.println(user2);

        sqlSession1.close();
    }

    @Test
    public void insertJnwjAndJnwjfj() throws Exception {
        try {
            String proptiesPath = "D:\\imageupload";
            String CLZL = "CLZL";
            String path = proptiesPath + "\\"+CLZL;
            File file = new File(path);
            if (file.isDirectory()) {
                System.out.println("文件夹路径："+path);
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    String WJID = UUID.randomUUID().toString().replaceAll("-","");
                    System.out.println("WJID:-->"+WJID);
                    String filePath = path + "\\" + filelist[i];
                    File readfile = new File(filePath);
                    System.out.println("AJID:-->"+readfile.getName());
                    System.out.println("TM:-->业务资料");
                    if (readfile.isDirectory()) {
                        String[] readfilelist = readfile.list();
                        System.out.println("YS:-->"+readfilelist.length);
                        for(int j=0;j<readfilelist.length;j++){
                            File pic = new File(filePath + "\\" + readfilelist[j]);
                            if(!pic.isDirectory()){
                                System.out.println("FJID:-->"+UUID.randomUUID().toString().replaceAll("-",""));
                                System.out.println("WJID:-->"+WJID);
                                System.out.println("FJMC:-->"+pic.getName());
                                // 转存
                                String disPath = "CLDA"+"\\"+filelist[i]+"\\"+readfilelist[j];
                                boolean res = copyFile(pic.getPath(),proptiesPath+"\\"+disPath);
                                if(res){
                                    System.out.println("PATH:-->"+disPath);
                                    System.out.println("FILETYPE:-->" + pic.getName().substring(pic.getName().lastIndexOf(".")));
                                    System.out.println("JNWJ_FJ_ID:-->" + UUID.randomUUID().toString().replaceAll("-",""));
                                    pic.delete();
                                }
                            }
                        }
                        if(readfile.list().length<1){
                            readfile.delete();
                        }
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("readfile()   Exception:" + e.getMessage());
        }
    }

    public  boolean copyFile(String oldPath, String newPath) throws IOException {
        boolean res = false;
        File oldFile = new File(oldPath);
        File file = new File(newPath);
        //获取父目录
        File fileParent = file.getParentFile();
        //判断是否存在
        if (!fileParent.exists()) {
            // 创建父目录文件
            fileParent.mkdirs();
        }
        file.createNewFile();
        FileInputStream in = new FileInputStream(oldFile);
        FileOutputStream out = new FileOutputStream(file);;
        try{
            byte[] buffer=new byte[2097152];
            while((in.read(buffer)) != -1){
                out.write(buffer);
            }
            res = true;
        }catch(IOException e){
            System.out.println(e.toString());
        }
        finally {
            in.close();
            out.close();
        }
        return res;
    }


    @Test
    public void writeFile() {
        String path = "D:\\imageupload\\log.txt";
        String content = "helloworld";
        File writefile;
        try {
            // 通过这个对象来判断是否向文本文件中追加内容
            // boolean addStr = append;
            writefile = new File(path);
            // 如果文本文件不存在则创建它
            if (!writefile.exists()) {
                writefile.createNewFile();
                writefile = new File(path); // 重新实例化
            }
            FileOutputStream fw = new FileOutputStream(writefile,true);
            Writer out = new OutputStreamWriter(fw, "utf-8");
            out.write(content);
            String newline = System.getProperty("line.separator");
            //写入换行
            out.write(newline);
            out.close();
            fw.flush();
            fw.close();
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}
